#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "ScreenUtils.h"
#include "ScreenImageProvider.h"
#include <memory>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    ScreenUtils *utils = new ScreenUtils();
    const QUrl url(u"qrc:/CaptionCover/Main.qml"_qs);
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
        &app, []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    //engine.addImageProvider(QLatin1String("ScreenImage"), new ScreenImageProvider());
    engine.addImageProvider(QLatin1String("ScreenImage"), utils->provider);
    engine.load(url);

    auto qmlRoot = engine.rootObjects().at(0);
    //std::shared_ptr<ScreenUtils> utilsPtr;
//    if(qmlRoot)
//    {
//        ScreenUtils::Initialize(qmlRoot, &app);
//    }
    QObject::connect(qmlRoot, SIGNAL(sig_capture()), utils, SLOT(slot_capture()));
    QObject::connect(qmlRoot, SIGNAL(sig_done(int,int)), utils, SLOT(slot_done(int,int)));
    QObject::connect(utils, SIGNAL(image_update()), qmlRoot, SIGNAL(sig_image_update()));
    QObject::connect(utils, SIGNAL(color_update(QColor)), qmlRoot, SIGNAL(sig_color_update(QColor)));

    QObject::connect(qmlRoot, SIGNAL(sig_copy_hex(QString)), utils, SLOT(slot_copy(QString)));
    QObject::connect(qmlRoot, SIGNAL(sig_copy_rgb(int,int,int)), utils, SLOT(slot_copy(int,int,int)));
    //QObject::connect(utils, SIGNAL(color_update(QColor)), qmlRoot, SLOT(color_update(QColor)));
    //connect(qmlRoot, SIGNAL(hideWindow()), utils, SLOT(onHideWindow()));
    //    QObject::connect(qmlRoot, SIGNAL(selectColor(int,int)), utils, SLOT(onSelectColor(int,int)));

    return app.exec();
}
